package br.com.sgo.dao;



import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.caelum.vraptor.ioc.Component;
import br.com.sgo.model.User;

@Component
public class UserDao {

	private final Session session;
	
	public UserDao(Session session) {
		this.session = session;
	}
	
	public User load(User user) {
		
		return (User) session.load(User.class, user.getId());
	}
	
	public User login(User user) {
		
		return (User) session.createCriteria(User.class).add(Restrictions.eq("email", user.getEmail())).add(Restrictions.eq("password", user.getPassword())).uniqueResult();
	}
	
	public void save(User user) {
		
		Transaction transaction = session.beginTransaction();
		try {
			session.save(user);
			transaction.commit();
		}
		catch(Exception e) {
			e.printStackTrace();
			transaction.rollback();
		}
		
	}
}
